import {needCLodop} from '../../Lodop';
import {Message} from 'element-ui';
let CreatedOKLodop7766 = null;
// ======获取LODOP对象的主过程：======
/**
 * @param oOBJECT
 * @param oEMBED
 * @returns {*}
 * @constructor
 */
function GetLodop (oOBJECT, oEMBED) {
  let LODOP;
  try {
    let isIE = (navigator.userAgent.indexOf('MSIE') >= 0) || (navigator.userAgent.indexOf('Trident') >= 0);
    if (needCLodop()) {
      try { LODOP = window.getCLodop(); } catch (err) {}
      if (!LODOP && document.readyState !== 'complete') {
        alert('C-Lodop没准备好，请稍后再试！');
        return;
      }
      if (!LODOP) {
        Message({
          type: 'error',
          message: 'CLodop云打印服务 未安装启动! 启动后，刷新页面，重新打印！',
          duration: 0,
        });
        return false;
      } else {
        if (window.CLODOP.CVERSION < '3.0.0.2') {
          Message({
            type: 'warning',
            message: 'CLodop 32 云打印服务需升级!请联系运维人员！',
          });
        }
        if (oEMBED && oEMBED.parentNode) oEMBED.parentNode.removeChild(oEMBED);
        if (oOBJECT && oOBJECT.parentNode) oOBJECT.parentNode.removeChild(oOBJECT);
      }
    } else {
      // =======如果页面有Lodop就直接使用，没有则新建:===============
      if (oOBJECT !== undefined || oEMBED !== undefined) {
        if (isIE) LODOP = oOBJECT;
        else LODOP = oEMBED;
      } else if (CreatedOKLodop7766 === null) {
        LODOP = document.createElement('object');
        LODOP.setAttribute('width', 0);
        LODOP.setAttribute('height', 0);
        LODOP.setAttribute('style', 'position:absoluteleft:0pxtop:-100pxwidth:0pxheight:0px');
        if (isIE) LODOP.setAttribute('classid', 'clsid:2105C259-1E0C-4534-8141-A753534CB4CA');
        else LODOP.setAttribute('type', 'application/x-print-lodop');
        document.documentElement.appendChild(LODOP);
        CreatedOKLodop7766 = LODOP;
      } else LODOP = CreatedOKLodop7766;
      // =======Lodop插件未安装时提示下载地址:===============
      if ((LODOP === null) || (typeof (LODOP.VERSION) === 'undefined')) {
        Message({
          type: 'error',
          message: 'CLodop云打印服务 未安装启动! 启动后，刷新页面，重新打印！',
          duration: 0,
        });
        return false;
      }
    }
    if (LODOP.VERSION < '6.2.1.8') {
      if (!needCLodop()) {
        if (window.is64IE) {
          Message({
            type: 'warning',
            message: 'CLodop 64 云打印服务需升级!请联系运维人员！',
          });
        } else {
          Message({
            type: 'warning',
            message: 'CLodop 32 云打印服务需升级!请联系运维人员！',
          });
        }
      }
      return LODOP;
    }
    // ====如下空白位置适合调用统一功能(如注册语句、语言选择等):====
    LODOP.SET_LICENSES('', '13528A153BAEE3A0254B9507DCDE2839', '', '');
    // ========================================================================================
    return LODOP;
  } catch (err) { alert('getLodop出错:' + err); }
}

export {
  GetLodop,
};
